/*
 * =====================================================================================
 *
 *       Filename:  test3.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  09/22/2018 12:59:57 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#include "json/json.h"

using namespace std;


#include<stdio.h>
#include<stdlib.h>
#include<curses.h>
//sudo apt-get install libncurses5-dev

//#define pri(fmt, ...) 	printf("["__FILE__"] <%s>_<%d> " ,__FUNCTION__,__LINE__ ); \
//						printf(fmt, ##__VA_ARGS__);



#include <stdio.h>
#include <ncurses.h>
#include <menu.h>
#include <string.h>
#include <stdlib.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

char *choices[] = {
    "Choice 1",
    "Choice 2",
    "Choice 3",
    "Choice 4",
    "Choice 5",
    "Choice 6",
    "Choice 7",
    "Choice 8",
    "Choice 9",
    "Choice 10",
    "Choice 11",
    "Choice 12",
    "Choice 13",
    "Choice 14",
    "Choice 15",
    (char *)NULL,
};

int initPanel()
{
    initscr();
    clear();
    if (has_colors() == FALSE)
    {
        endwin();
        printf("You terminal dose not support color\n");
        exit(1);
    }

    start_color();
    cbreak();
    keypad(stdscr, TRUE);
    noecho();
    curs_set(0);

    init_pair(1, COLOR_WHITE, COLOR_BLUE); /* normal */
    init_pair(2, COLOR_WHITE, COLOR_GREEN); /* hightlight */
    init_pair(3, COLOR_WHITE, COLOR_MAGENTA); /* object */
    init_pair(4, COLOR_WHITE, COLOR_YELLOW); /* select */

    return 0;
}

int wgetKey(WINDOW *win)
{
    return wgetch(win);
}

int drawMenu(int height, int width, int starty, int startx)
{
    ITEM **items;
    MENU *menu;
    WINDOW *menu_win;
    int n_choices, i;
    int ch;

    n_choices = ARRAY_SIZE(choices);
    items = (ITEM**)calloc(n_choices, sizeof(ITEM *));
    for (i = 0; i < n_choices; i++)
        items[i] = new_item(choices[i], /*choices[i]*/NULL);
    menu = new_menu(items);

    menu_win = newwin(height, width, starty, startx);
    keypad(menu_win, TRUE);
    for(i = 0; i < LINES; i++)
        mvwchgat(menu_win, i, 0, -1, A_BOLD, 1, NULL);

    set_menu_win(menu, menu_win);
    set_menu_sub(menu, derwin(menu_win, height, width, 0, 0));
    set_menu_format(menu, 8, 1);
    set_menu_mark(menu, 0);
    set_menu_fore(menu, COLOR_PAIR(2) | A_BOLD);
    set_menu_back(menu, COLOR_PAIR(3) | A_BOLD);

    menu_opts_off(menu, O_ONEVALUE);
    menu_opts_off(menu, O_SHOWDESC);

    post_menu(menu);
    wrefresh(menu_win);
    
    bool blbreak=false;
    while (blbreak==false)
    {
        ch = wgetKey(menu_win);
        switch (ch)
        {
            case KEY_LEFT:
                menu_driver(menu, REQ_UP_ITEM);
                break;
            case KEY_RIGHT:
                menu_driver(menu, REQ_DOWN_ITEM);
                break;
            case ' ':
                menu_driver(menu, REQ_TOGGLE_ITEM);
                break;
            case KEY_ENTER:
                break;
            //case KEY_:
            //	blbreak=true;
            //   break;              
        }
        wrefresh(menu_win);
    }
    unpost_menu(menu);
    free_menu(menu);
    for (i = 0; i < n_choices; i++)
        free_item(items[i]);

    return 0;
}

int main(void)
{
    initPanel();

    int i;
    for(i = 0; i < LINES; i++)
        mvchgat(i, 0, -1, A_BOLD, 1, NULL);

    drawMenu(20,20,10,10);
    refresh();

    getch();
    endwin();
    return 0;
}


int main55()
{
initscr(); /*初始化屏幕*/
if(start_color() == OK) /*开启颜色*/
{
init_pair(1, COLOR_RED, COLOR_GREEN); /*建立一个颜色对*/
attron(COLOR_PAIR(1)); /*开启字符输出颜色*/
move(LINES/2, COLS/2);
waddstr(stdscr, "Yet another Hello, world!");
attroff(COLOR_PAIR(1)); /*关闭颜色显示*/
refresh();
}
else
{
waddstr(stdscr, "Can not init color");
refresh();
}
endwin(); /*关闭curses状态*/
return 0;
}


int main44()
{
initscr(); /*初始化屏幕*/
if(start_color() == OK) /*开启颜色*/
{
init_pair(1, COLOR_RED, COLOR_GREEN); /*建立一个颜色对*/
attron(COLOR_PAIR(1)); /*开启字符输出颜色*/
move(LINES/2, COLS/2);
waddstr(stdscr, "Yet another Hello, world!");
attroff(COLOR_PAIR(1)); /*关闭颜色显示*/
refresh();
}
else
{
waddstr(stdscr, "Can not init color");
refresh();
}
endwin(); /*关闭curses状态*/
return 0;
} 

int main33()
{
initscr();

box(stdscr, ACS_VLINE, ACS_HLINE); /*draw a box*/
move(LINES/2, COLS/2); /*move the cursor to the center*/
waddstr(stdscr, "Hello, world!"); 
refresh();
getch();

endwin();
return 0;
} 

#if 0
int main22()
{
	pri(" ok \n");
	initscr();
	cbreak();
	noecho();

	if (has_colors() == FALSE)
		{
			pri(" ok \n");
			endwin();
			pri("Your terminal does not support color\n");
			exit(1);
		}
	endwin();	
	pri("Your terminal support color !\n");
	return 0;
}
#endif

int main11()
{
	Json::CharReaderBuilder rbuilder;
	Json::CharReader* reader = rbuilder.newCharReader();

	rbuilder["collectComments"] = false;

	Json::Value root_group;
	JSONCPP_STRING errs;

	fstream f;
	//f.open("Personal-details.json", ios::in);
	f.open("json_ec_io.txt", ios::in);

	if(!f.is_open())
	{
		cout << "Open json file error!" << endl;
	}

	ostringstream os;
	os << f.rdbuf();
	string s = os.str();
	os.clear();
	os.str("");
	f.seekg(0);

	bool parse_ok = reader->parse(s.data(), s.data()+s.size(), &root_group, &errs);
	if(!parse_ok)
	{
		cout << "Parse json file error!" << endl;
	}
	else
	{
		cout << "Printf Eva's information:" << endl;
		//cout << root_group.toStyledString() << endl;
		try{
			//cout << "Personal-details:Name=" << root_group["Personal-details"][1]["Name"]<< endl;
			//cout << "Personal-details:Age=" << root_group["Personal-details"][1]["Age"]<< endl;
			//cout << "Personal-details:Live-City=" << root_group["Personal-details"][1]["Live-City"]<< endl;
			//cout << "Personal-details:Phone-Number=" << root_group["Personal-details"][1]["Phone-Number"]<< endl;
			//cout << "Personal-details:Phone-Number:1=" << root_group["Personal-details"][1]["Phone-Number"]["1"]<< endl;
			//cout << "Personal-details:Phone-Number:2=" << root_group["Personal-details"][1]["Phone-Number"]["2"]<< endl;
			//cout << "Personal-details:Index=" << root_group["Personal-details"][1]["Index"]<< endl;

			cout << "test_led_on_off:isTest=" << root_group["test_led_on_off"]["isTest"]<< endl;
			cout << "test_power_button:isTest=" << root_group["test_power_button"]["isTest"]<< endl;

			{
				bool isTest=root_group["test_led_on_off"]["isTest"].asBool();
				cout << "test_led_on_off:isTest=" << isTest << endl;
				if(true==isTest)
				{
					char *ptr;
					unsigned long ul;
					ul = std::stoul (root_group["test_led_on_off"]["addr"].asString(),nullptr,16);
					cout << "test_led_on_off:addr=0x" << std::hex << ul << endl;
					ul = std::stoul (root_group["test_led_on_off"]["len"].asString(),nullptr,16);
					cout << "test_led_on_off:len=0x" << std::hex << ul << endl;
					//cout << "test_led_on_off:addr=0x" << std::hex << strtoul(root_group["test_led_on_off"]["addr"].asString().c_str(),&ptr,16)<< endl;
					//cout << "test_led_on_off:len=" << strtoul(root_group["test_led_on_off"]["len"].c_str(),&ptr,16)<< endl;

					bool isOn;
					isOn=root_group["test_led_on_off"]["result_led_power"]["on"].asBool();
					cout << "test_led_on_off:result_led_power:on=" << isOn << endl;

					isOn=root_group["test_led_on_off"]["result_led_charge"]["on"].asBool();
					cout << "test_led_on_off:result_led_charge:on=" << isOn << endl;


				}				
			}
			{
				bool isTest=root_group["test_power_button"]["isTest"].asBool();
				cout << "test_power_button:isTest=" << isTest << endl;
				if(true==isTest)
				{
					char *ptr;
					unsigned long ul;
					ul = std::stoul (root_group["test_power_button"]["addr"].asString(),nullptr,16);
					cout << "test_power_button:addr=0x" << std::hex << ul << endl;
					ul = std::stoul (root_group["test_power_button"]["len"].asString(),nullptr,16);
					cout << "test_power_button:len=0x" << std::hex << ul << endl;					
					//char *ptr;
					//cout << "test_power_button:addr=0x" << strtoul(root_group["test_power_button"]["addr"].c_str(),&ptr,16)<< endl;
					//cout << "test_power_button:len=0x" << strtoul(root_group["test_power_button"]["len"].c_str(),&ptr,16)<< endl;
				}
			}

		}catch(const Json::LogicError &ex)
			{
				cout << "Parse json string error!" << endl;
			}
	}

	f.close();

	return 0;
}
